<?php


namespace app\api\controller;

use app\common\controller\Api;
use app\common\controller\OperationLog;
use think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\Exception;
use think\exception\DbException;
use think\Request;

/**
 * Class Config
 * @package app\api\controller
 */
class Config extends Api
{
    /**
     * 不需要登录权限的方法数组
     * @var string[]
     */
    protected $noNeedLogin = ['get_index_data'];
    /**
     * 不需要鉴权的方法数组
     * @var array
     */
    protected $noNeedAuth = ['get_index_data'];
    /**
     * 不需要验证token的方法数组
     * @var array
     */
    protected $noNeedToken = ['get_index_data'];

    /**
     * 获取首页配置
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function get_index_data()
    {
        // 定义日志参数
        $options = array(
            'title' => '获取首页配置',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
        );
        $config = new \app\api\model\Config();
        $configData = [];
        $configData['icp'] = $config::get(['name' => 'icp']);
        $configData['qq_app_id'] = $config::get(['name' => 'qq_app_id']);
        $configData['gaba'] = $config::get(['name' => 'gaba']);
        OperationLog::OperSuccess([], $options, 'R', $configData);
        return $this->success('success', $configData);
    }

    /**
     * 获取ICP配置
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function get_icp_config()
    {
        // 定义日志参数
        $options = array(
            'title' => '获取ICP配置',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
        );
        $where = [];
        $where['name'] = ['=', 'icp'];
        $data = \app\api\model\Config::where($where)->find();
        OperationLog::OperSuccess([], $options, 'R', $data);
        return $this->success('success', $data);
    }

    /**
     * 修改icp配置
     * @throws DbException
     */
    public function modify_icp_config()
    {
        // 定义日志参数
        $options = array(
            'title' => '修改ICP配置',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
        );
        $param = Request::instance()->post() ?? [];
        $icp = \app\api\model\Config::get(['id', '=', $param['id']]);
        if (empty($icp)) {
            OperationLog::OperError($param, $options, 'U', 'ICP配置不存在', 'ICP配置不存在');
            $this->error('error', ['code' => 201, 'data' => 'ICP配置不存在']);
        }
        Db::startTrans();
        try {
            $icp->value = $param['value'];
            $icp->save();
            Db::commit();
            OperationLog::OperSuccess($param, $options, 'U', '操作成功');
            $this->success('success', ['code' => 200, 'data' => '操作成功']);
        } catch (Exception $e) {
            Db::rollback();
            OperationLog::OperError($param, $options, 'U', $e->getMessage(), $e->getMessage());
            $this->error('error',$e->getMessage(),202);
        }
    }

    /**
     * 获取公网安备数据
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function get_gaba_config()
    {
        // 定义日志参数
        $options = array(
            'title' => '获取公网安备数据',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
        );
        $where = [];
        $where['name'] = ['=', 'gaba'];
        $data = \app\api\model\Config::where($where)->find();
        OperationLog::OperSuccess([], $options, 'R', $data);
        return $this->success('success', $data);
    }

    /**
     * 修改公安备案配置接口
     * @throws DbException
     */
    public function modify_gaba_config()
    {
        // 定义日志参数
        $options = array(
            'title' => '修改公网安备配置',
            'method' => Request::instance()->action(),
            'request_method' => Request::instance()->method(),
            'open_url' => Request::instance()->url(),
            'oper_ip' => Request::instance()->ip(),
        );
        $param = Request::instance()->post() ?? [];
        $gaba = \app\api\model\Config::get(['id', '=', $param['id']]);
        if (empty($gaba)) {
            OperationLog::OperError($param, $options, 'U', '公网安备配置不存在', '公网安备配置不存在');
            $this->error('error', ['code' => 201, 'data' => '公网安备配置不存在']);
        }
        Db::startTrans();
        try {
            $gaba->value = $param['value'];
            $gaba->save();
            Db::commit();
            OperationLog::OperSuccess($param, $options, 'U', '操作成功', '操作成功');
            $this->success('success', ['code' => 200, 'data' => '操作成功']);
        } catch (Exception $e) {
            Db::rollback();
            OperationLog::OperError($param, $options, 'U', $e->getMessage(), $e->getMessage());
            $this->error('error', $e->getMessage(),202);
        }
    }
}